const { models } = require('mongoose');
const jobsModels =require('../models/Job')
const {formatTime} = require('../utils/formatTime')


class jobsService{
    async getAllJobs(){
        const result = await jobsModels.findAllJob();
        return formatTime(result);
    }

    async findJob(data){
        return await jobsModels.findJob(data);
    }

    async createJob(jobData){
        if( !jobData){
            throw new Error('username and password is required');
        }
        return await jobsModels.createJob(jobData);
    }

    async getJobById(id){
        const result = await jobsModels.finById(id);
        if(!result){
            throw new Error('Not this jos message!')
        }
        return formatTime(result);
    }

    async updateaJob(id,jobData){
        const result = await jobsModels.updateJob(id,jobData);
        if (!result) {
            throw new  Error('没有找到该id的岗位信息')
        }
        return { id, ...jobData};
    }


    async deleteJob(id){
        const result = await jobsModels.deleteJob(id);
        if (!result){
            throw new Error('没有找到该id的岗位信息')
        }
    }

    async filtrateJobs(filters){
        const result =await jobsModels.filtrateJobs(filters);
        if(!result) {
            throw new Error('没有找到岗位信息')
        }
        return result;
    }
}



module.exports = new jobsService();